//
//  EnergyCurrencyView.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/26.
//

#import "EnergyCurrencyView.h"

@interface EnergyCurrencyView()
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic,strong) NSString * coinStr;
@property (nonatomic, assign) CFTimeInterval pausedTime;

@end

@implementation EnergyCurrencyView


-(instancetype)initWithFrame:(CGRect)frame{
    
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self creatAnimation];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotification:) name:@"StartAnimation" object:nil];
    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(20 * FMScaleX, 20 * FMScaleX));
    }];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconViewTap)];
    [self addGestureRecognizer:tap];
}

-(void)setID:(NSString *)ID{
    
    self.coinStr = ID;
    
}

-(void)iconViewTap{
        
    [SPNetworkingIns POST:PORT(@"monetary-center/api/herbCoin/collectHerbCoin") params:@{@"id":@[self.coinStr]} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        FhudShowStr(@"收取成功");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"RefshCoin" object:@"" userInfo:nil];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
    
}

-(void)getNotification:(id)sender{
    [self startAnimation];
}

-(void)pauseAnimation{
    
    self.pausedTime = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    self.iconView.layer.speed = 0.0;
    self.iconView.layer.timeOffset = self.pausedTime;
}
-(void)startAnimation{
    
    CFTimeInterval pausedTime = self.iconView.layer.timeOffset;
    self.iconView.layer.speed = 1.0;
    self.iconView.layer.timeOffset = 0.0;
    self.iconView.layer.beginTime = 0.0;
    CFTimeInterval timeSincePause = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    self.iconView.layer.beginTime = timeSincePause;
    
}

- (void)creatAnimation {
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = @(1.0);
    scaleAnimation.toValue = @(1.5);
    scaleAnimation.duration = 1.0;
    scaleAnimation.autoreverses = YES;
    scaleAnimation.repeatCount = HUGE_VALF;
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    [self.iconView.layer addAnimation:scaleAnimation forKey:@"scaleAnimation"];
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"Energy_info_currency"];
      
    }
    return _iconView;
}
@end
